/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.enums.WebConfigTypeEnum;
import com.yami.shop.bean.model.WebConfig;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.dao.WebConfigMapper;
import com.yami.shop.service.SysConfigService;
import com.yami.shop.service.WebConfigService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author SJL
 * @date 2021-02-20 09:44:42
 */
@Service
@AllArgsConstructor
public class WebConfigServiceImpl extends ServiceImpl<WebConfigMapper, WebConfig> implements WebConfigService {

    private SysConfigService sysConfigService;
    private final ShopConfig shopConfig;

    @Override
    @Cacheable(cacheNames = "ActivityWebConfig", key = "#paramKey")
    public WebConfig getActivityWebConfig(String paramKey) {
        String webConfigValue = sysConfigService.getValue(paramKey);
        WebConfig webConfig = JSONObject.parseObject(webConfigValue, WebConfig.class);
        if (webConfig != null) {
            webConfig.setParamKey(paramKey);
            spliceImgPre(webConfig);
        }
        return webConfig;
    }


    /**
     * 增加图片资源前缀
     *
     * @param webConfig
     * @return
     */
    private void spliceImgPre(WebConfig webConfig) {
        String resourcesUrl = shopConfig.getDomain().getResourcesDomainName() + "/";

        if (Objects.equals(webConfig.getParamKey(), WebConfigTypeEnum.PLATFROM.value()) || Objects.equals(webConfig.getParamKey(), WebConfigTypeEnum.MULTISHOP.value())) {
            // 后台管理的图片
            webConfig.setBsLoginBgImg(StringUtils.isEmpty(webConfig.getBsLoginBgImg()) ? null : resourcesUrl + webConfig.getBsLoginBgImg());
            webConfig.setBsLoginLogoImg(StringUtils.isEmpty(webConfig.getBsLoginLogoImg()) ? null : resourcesUrl + webConfig.getBsLoginLogoImg());
            webConfig.setBsTitleImg(StringUtils.isEmpty(webConfig.getBsTitleImg()) ? null : resourcesUrl + webConfig.getBsTitleImg());
            webConfig.setBsTopBarIcon(StringUtils.isEmpty(webConfig.getBsTopBarIcon()) ? null : resourcesUrl + webConfig.getBsTopBarIcon());
        } else if (Objects.equals(WebConfigTypeEnum.PC.value(), webConfig.getParamKey())) {
            // PC端的图片
            webConfig.setPcLogoImg(StringUtils.isEmpty(webConfig.getPcLogoImg()) ? null : resourcesUrl + webConfig.getPcLogoImg());
            webConfig.setPcLogoImgText(StringUtils.isEmpty(webConfig.getPcLogoImgText()) ? null : resourcesUrl + webConfig.getPcLogoImgText());
            webConfig.setPcTitleImg(StringUtils.isEmpty(webConfig.getPcTitleImg()) ? null : resourcesUrl + webConfig.getPcTitleImg());
            webConfig.setPcQrcodeImg(StringUtils.isEmpty(webConfig.getPcQrcodeImg()) ? null : resourcesUrl + webConfig.getPcQrcodeImg());
            webConfig.setPcLoginBgImg(StringUtils.isEmpty(webConfig.getPcLoginBgImg()) ? null : resourcesUrl + webConfig.getPcLoginBgImg());

        } else if (Objects.equals(WebConfigTypeEnum.H5.value(), webConfig.getParamKey())) {
            // H5端的图片
            webConfig.setUniLoginLogoImg(StringUtils.isEmpty(webConfig.getUniLoginLogoImg()) ? null : resourcesUrl + webConfig.getUniLoginLogoImg());

        } else if (Objects.equals(WebConfigTypeEnum.STATION.value(), webConfig.getParamKey())) {
            // 自提点端的图片
            webConfig.setStationLoginLogoImg(StringUtils.isEmpty(webConfig.getStationLoginLogoImg()) ? null : resourcesUrl + webConfig.getStationLoginLogoImg());
        }
    }
}
